'use strict';

const Service = require('egg').Service;

class MessageService extends Service {
  //获取信息列表
  async getInfoList() {
    try {
      return await this.app.model.Message.findAll();
    } catch (error) {
      return null;
    }
  }
  //发表单条信息
  async createMessage(body) {
    try {
      const message = {
        user_id: body.user_id,
        username: body.username,
        face_url: body.face_url,
        content: body.content,
        total_likes: body.total_likes,
        send_timestamp: body.send_timestamp,
      }
      console.log(message);
      await this.app.model.Message.create(message);
      return {
        "error_code": 0,
        "msg": "发表成功",
      }
    } catch (error) {
      console.log("错误");
      return false;
    }
  }
  async getMessageDetail(query) {
    try {
      // const nember = parseInt(query.page);
      // const start = nember * 10 - 10;
      // const degree = parseInt(query.total);
      // const IdMessageList = await this.app.model.Message.findAll({
      //   limit: [start, degree],
      //   'order': [
      //     ['orderby', 'asc']
      //   ],
      // })
      // console.log(query);
      const IdMessageList = await this.app.model.Message.findAll({
        where:{
          user_id:query
        }
      })
      return IdMessageList;
    } catch (error) {
      return null;
    }
  }
  async deleteIdMessage(id){
    try {
      await this.app.model.Message.destroy({
        where:{
          id
        }
      })
      return true
    } catch (error) {
      return false;
    }
  }
}

module.exports = MessageService;
